package com.xiangban.springTask;

import com.alibaba.fastjson.JSON;
import com.xiangban.dao.TUserMappers;
import com.xiangban.feign.UserServiceFeign;
import com.xiangban.model.TUser;
import com.xiangban.model.res.ApiResult;
import com.xiangban.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.xiangban.util.CommonUtil.BizEmpty;


/**
 * @Author Mr. MaYaYun
 * @Data 2021/11/8 16:37
 */
@Slf4j
@Component
@EnableScheduling
public class TimedTask {

    @Autowired
    private UserServiceFeign userServiceFeign;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TUserMappers userMapper;

    @Scheduled(initialDelay = 10000,fixedRate = 1000*60*60*24)
    public void task(){
        ApiResult<List<TUser>> userList = userServiceFeign.getUserList();
        String lock = redisUtil.get("run_batch_lock");
        if (BizEmpty(lock) || lock.equals("true")){
            redisUtil.set("run_batch_lock","false",-1);
            if (userList.isSuccess()){
                List<TUser> dataMap = userList.getDataMap();
                if (dataMap != null){
                    for (TUser tUser : dataMap) {
                        TUser tUser1 = userMapper.getUserInfoByUid(tUser);
                        if (tUser1 == null){
                            userMapper.addUser(tUser);
                            log.info(JSON.toJSONString(tUser));

                        }
                    }
                }
            }
        }
        redisUtil.set("run_batch_lock","true",-1);


    }


}
